package com.kfm.base.thread.day03;

import java.time.LocalTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class Demo05 {

    public static void main(String[] args) {
        // 可缓存线程池, 空闲线程 60秒后回收
        ExecutorService executorService = Executors.newCachedThreadPool(new MyThreadFactory());

        for (int i = 0; i < 10; i++) {

            int num = i;
            executorService.execute(() -> {
                System.out.println("当前时间:" + LocalTime.now() + ", 线程: " + Thread.currentThread().getName() + ", 序号：" + num);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
        }

        executorService.shutdown();
//        List<Runnable> runnables = executorService.shutdownNow();
//
//        System.out.println(runnables);
    }
}

class MyThreadFactory implements ThreadFactory {

    int count = 0;
    @Override
    public Thread newThread(Runnable r) {
        return new MyThread(r, "线程" + count, count ++);
    }
}

class MyThread extends Thread{

    int index;

    public MyThread(Runnable runnable, String name, int index){
        super(runnable, name);
        this.index = index;
    }

    @Override
    public String toString() {
        return super.toString() + ", index:" + this.index;
    }
}
